# -*- coding: utf-8 -*-
# 导如第三方库
import pandas as pd

from pyecharts.charts import Bar, Map,Timeline

from pyecharts import options as opts

# 2019财富排行榜top10
# 读取csv数据
data = pd.read_csv('rich.csv', encoding='utf-8', usecols=["year", "rank", "name", "money","city"])

row = data.loc[data["year"] == 2019]

name = row[:10]["name"].to_dict()

money = row[:10]["money"].to_dict()

# 引入pyecharts柱状图
bar = Bar()

# 添加图表x轴数据
bar.add_xaxis(list(name.values()))

# 添加图表y轴数据
bar.add_yaxis("个人财富", [float(i.replace(",","")) for i in list(money.values())])

# 设置图表标题信息
bar.set_global_opts(title_opts=opts.TitleOpts(title="2019个人财富排行TOP10", subtitle="财富"))

# 绘制图表
bar.render('rank.html')

# 年份时空分析
tl = Timeline()

for i in [2017,2018,2019]:
    row = data.loc[data["year"] == i]

    name = row[:10]["name"].to_dict()

    money = row[:10]["money"].to_dict()

    # 生成pyecharts图表实例
    bar = Bar()

    # 添加图表x轴数据
    bar.add_xaxis(list(name.values()))

    # 添加图表y轴数据
    bar.add_yaxis("个人财富", [float(i.replace(",","")) for i in list(money.values())])

    # 设置图表标题信息
    bar.set_global_opts(title_opts=opts.TitleOpts(title="{}年财富榜TOP10".format(i), subtitle="亿/美元"))

    tl.add(bar, "{}年".format(i))

tl.render('time.html')

# 空间分析（富豪所在地分析）
map = row = data.loc[data["year"] == 2019]["city"].value_counts().to_dict()

city = [i+"市" for i in map.keys()]

map0 = (
    Map()
        .add("地区", [list(z) for z in zip(city, map.values())], "广东", is_map_symbol_show=False)  # 以列表形式存放数据
        .set_global_opts(
        title_opts=opts.TitleOpts(title="2019年度广东富豪所在城市分布"),
        visualmap_opts=opts.VisualMapOpts(max_=200),
    ).render('map.html')
)

#  时空地图
year = data['year'].value_counts().to_dict().keys()

year = list(year)

year.sort()

tl = Timeline()

for i in [2018,2019]:
    map = data.loc[data["year"] == i]["city"].value_counts().to_dict()

    city = [i+"市" for i in map.keys()]

    map0 = (
        Map()
            .add("", [list(z) for z in zip(city, map.values())], "广东", is_map_symbol_show=False)  # 以列表形式存放数据
            .set_global_opts(
            title_opts=opts.TitleOpts(title="{}年度广东富豪所在城市分布".format(i)),
            visualmap_opts=opts.VisualMapOpts(max_=200),
        )
    )

    tl.add(map0, "{}年".format(i))

tl.render('shikong.html')
